﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AAA.Database;
using System.Data.Common;
using System.Windows.Forms;

namespace AAA.ResultSet
{
    public class DatabaseResultSet : AbstractResultSet
    {
        private DatabaseTypeEnum _eDatabaseType;
        private string _strHost;
        private string _strDatabase;
        private string _strUsername;
        private string _strPassword;
        private string _strSQLStatement;

        public DatabaseResultSet(DatabaseTypeEnum eDatabaseType, string strHost, string strDatabase, string strUsername, string strPassword)
        {
            _eDatabaseType = eDatabaseType;
            _strHost = strHost;
            _strDatabase = strDatabase;
            _strUsername = strUsername;
            _strPassword = strPassword;
        }

        public string SQLStatement
        {
            get { return _strSQLStatement; }
            set { _strSQLStatement = value; }
        }

        public override bool Load()
        {
            IDatabase database = null;
            DbDataReader dataReader = null;
            object[] oValues;

            if (SQLStatement == null)
            {
                ErrorMessage = "SQL statement is null";
                IsSuccess = false;
                return IsSuccess;
            }

            try
            {
                switch (_eDatabaseType)
                {
                    case DatabaseTypeEnum.MSSql:
                        database = new MSSqlDatabase();
                        database.Open(_strHost, _strDatabase, _strUsername, _strPassword);
                        break;
                    case DatabaseTypeEnum.Access:
                        database = new AccessDatabase();
                        database.Open(_strDatabase, _strUsername, _strPassword);
                        break;
                }

                database.ErrorMessage = null;
                dataReader = database.ExecuteQuery(SQLStatement);
                if (database.ErrorMessage != null)
                {
                    ErrorMessage = database.ErrorMessage;
                    return false;
                }
                for (int i = 0; i < dataReader.FieldCount; i++)
                    AddColumn(dataReader.GetName(i));

                while (dataReader.Read())
                {
                    if (IsDoEvents)
                        Application.DoEvents();

                    oValues = new object[dataReader.FieldCount];

                    for (int i = 0; i < dataReader.FieldCount; i++)
                        oValues[i] = dataReader[dataReader.GetName(i)].ToString();

                    AddRow(oValues);
                }

                IsSuccess = true;
            }
            catch (Exception ex)
            {
                ErrorMessage = ex.Message + "," + ex.StackTrace;
                IsSuccess = false;
            }
            finally
            {
                if(dataReader != null)
                    dataReader.Close();

                if (database != null)
                    database.Close();
            }
            return IsSuccess;
        }
    }
}
